<!--
 * @Author: long
 * @Date: 2021-09-06 19:34:13
 * @LastEditTime: 2021-10-14 11:38:12
 * @LastEditors: Please set LastEditors
 * @Description: 应用管理-日志统计
 * @FilePath: \ant-design-vue-jeecge:\hjimi\人脸辨识云\html\gitlab\pc\faceCloudWebsite\src\views\application-manage\application-log\components\LogCharts.vue
-->
<style lang="scss" scoped>
.log {
  margin: 0 16px
}
</style>
<template>
  <div
    class="log"
    :class="className"
    :style="{ height: height, width: width }"
  >
  </div>
</template>

<script>
import echarts from "echarts"
import resize from "@/utils/mixins/resize"
import { serveLog } from "@/api/application-manage/uLog"

const INITDATA = ["暂无数据"]

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "445px",
    },
    form: {
      type: Object,
      required: true,
    }
  },
  data() {
    return {
      chart: null,
      xValue: INITDATA,
      ylValue: INITDATA,
      yrValue: INITDATA,
      isWatch: false,
      toolColor: '#1890FF'
    }
  },
  watch: {
    form: {
      handler() {
         if(this.isWatch) {
           this.xValue = this.ylValue = this.yrValue = INITDATA
           this.getLogsData()
         }
      },
      deep: true,
      immediate: true
    },
  },
  methods: {
    async getLogsData() {
      this.$emit('chartLoadShow')
      let { unit } = this.form
       this.setChartOptions()
       await serveLog(this.form).then((res) => {
          if (res.code == 0) {
            this.$emit('chartLoadHide')
            if(res.data ?? '' !== '') {
              const { dates, accessCounts, qpsMaxCounts } = res.data
            if(dates && accessCounts && qpsMaxCounts) {
              if(unit === 'HOUR') {
                this.xValue = dates.map((item) => item + ':00')
              } else {
                this.xValue = dates
              }
              this.ylValue = accessCounts
              this.yrValue = qpsMaxCounts
              this.setChartOptions()
              this.isWatch = true
            }
            }
          }
        })
    },
    async initChart() {
      Promise.resolve().then(() => {
        this.chart = echarts.init(this.$el)
        this.setChartOptions()
      })
    },
    setChartOptions() {
      this.chart.clear()
      this.chart.setOption({
        title: {
          text: '时间',
          left: "center",
          bottom: 0,
          textStyle: {
            fontSize: 14
          }
        },
        tooltip: {
          show: true,
          padding: 10,
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        toolbox: {
          feature: {
            dataView: { 
              show: true, 
              readOnly: false,
              emphasis: {
                 iconStyle: {
                   borderColor: this.toolColor
                 }
               }
              },
            magicType: { 
              show: true, 
              type: ["line", "bar"],
               emphasis: {
                 iconStyle: {
                   borderColor: this.toolColor
                 }
               }
              },
            restore: { 
              show: true,
               emphasis: {
                 iconStyle: {
                   borderColor: this.toolColor
                 }
               }
              },
            saveAsImage: { 
              show: true,
              emphasis: {
                 iconStyle: {
                   borderColor: this.toolColor
                 }
               }
              },
          },
        },
        legend: {
          data: ["调用总量", "QPS峰值"],
        },
        calculable: true,
        xAxis: [
          {
            type: "category",
            data: this.xValue,
            axisPointer: {
              type: "shadow",
               show: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "      QPS(次/秒)",
            min: 0,
            // max: 250,
            interval: 2,
            axisTick: {
              show: true,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
            axisLabel: {
              formatter: "{value}",
            },
          },
          {
            type: "value",
            name: "调用总量",
            min: 0,
            // max: 25,
            interval: 5,
            axisTick: {
              show: true,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
            axisLabel: {
              formatter: "{value}",
            },
          },
        ],
        series: [
          {
            name: "调用总量",
            itemStyle: {
              color: "#40a9ff",
              borderWidth: 5,
            },
            type: "bar",
            data: this.yrValue,
            barWidth: 10,
            animationDuration: 700,
            animationEasing: "cubicInOut",
             markLine: {
                data: [
                    {type: 'average', name: '平均值'}
                ]
            }
            // stack: "vistors",
          },
          {
            name: "QPS峰值",
            type: "line",
            barWidth: 10,
            itemStyle: {
              color: "#FA8C16",
            },
            yAxisIndex: 1,
            data: this.ylValue,
            animationDuration: 700,
            animationEasing: "cubicInOut",
          },
        ],
        grid: {
          left: 5,
          right: 15,
          bottom: 35,
          top: 71,
          containLabel: true
        },
      })
    },
  },
  created() {
      setTimeout(() => {
        this.getLogsData()
      }, 800)
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    setTimeout(() => {
     this.chart.dispatchAction({
     type: 'showTip',
     seriesIndex: 0 ,
     dataIndex: 0,
     })
   },1000)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  }
}
</script>